package com.spiden.bazaspiden.processor;

import com.spiden.bazaspiden.pipeline.OschinaPipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * @author 张高昌
 * @date 2024/1/4 19:42
 * @description: 简单的爬虫
 */
public class OschinaBlogPageProcessor implements PageProcessor {

    private Site site = Site.me().setDomain("my.oschina.net");

    @Override
    public void process(Page page) {
        List<String> links = page.getHtml().links().regex("https://my\\.oschina\\.net/flashsword/blog/\\d+").all();
        page.addTargetRequests(links);

        page.putField("title", page.getHtml().xpath("//*[@id=\"mainScreen\"]/div[2]/div[1]/div[1]/div[2]/div[1]/div/div[1]/div/div[1]/h1/a").toString());
        page.putField("content", page.getHtml().xpath("//*[@id=\"mainScreen\"]/div[2]/div[1]/div[1]/div[2]/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/div").toString());
    }

    @Override
    public Site getSite() {
        return site;

    }

    public static void main(String[] args) {
        Spider.create(new OschinaBlogPageProcessor()).addUrl("https://my.oschina.net/flashsword/blog")
                .addPipeline(new OschinaPipeline()).run();
    }
}
